/*
锻造
*/
window._MFORGIN = {

    _CURRENTLABEL:null,
    _SOURCE:null,
    _CELL1:null,
    _CELL2:null,
    _CELL3:null,
    _CELL4:null,
    _MAKETYPE:null,//1--装备 2--道具
    _CURRENTPARAM:null,

    showForgin:function(parentNode,param){
        var uiObj = _GRM.getNodeObj('forging');
        var mainLabel = uiObj.node;

        _MFORGIN._CURRENTLABEL = mainLabel;
        _MFORGIN._CURRENTPARAM = param;

        if(uiObj.isnew){
            _MFORGIN.initButton(mainLabel);
        }
        _MFORGIN.clear();
        _MFORGIN.resetCell();
        parentNode.addChild(mainLabel);
        return mainLabel;
    },

    initButton:function(mainLabel){
        mainLabel.getChildByName('mainLabel').getChildByName('confirm')
        .on(cc.Node.EventType.TOUCH_START, function(){
            _MFORGIN.makeObj();
        }, mainLabel);
    },

    makeObj:function(){

        if(_MFORGIN._SOURCE==null){
            _TIPS.showMessage('请先选择制作书',0);
            return;
        }

        if(_MFORGIN._MAKETYPE==1){
            if(_MFORGIN._CELL3==null||_MFORGIN._CELL4==null){
                _TIPS.showMessage('请选择原材料和打造石',0);
                return;
            }
        }

        var makeconfig = _MAKE[_MFORGIN._SOURCE].arr;
        for(var i=0;i<makeconfig.length;i++){

            if(makeconfig[i]
                &&makeconfig[i].nmb>0
                &&_MFORGIN['_CELL'+(i+1)]
                ){

                if(_MFORGIN['_CELL'+(i+1)].index==-1){
                    _TIPS.showMessage(makeconfig[i].type+'数量不足！',0);
                    return;
                }

                var tempcell = _MFORGIN['_CELL'+(i+1)];
                var goodsNmb = tempcell.arr[tempcell.index].nmb?tempcell.arr[tempcell.index].nmb:1;

                if(makeconfig[i].nmb>goodsNmb){
                    _TIPS.showMessage(makeconfig[i].type+'数量不足！',0);
                    return;
                }

            }

        }

        

        if(_MFORGIN._MAKETYPE==1){

            var equipInfo = _MFORGIN._CELL3.arr[ _MFORGIN._CELL3.index];
            var mtype = window.gameData.materialItem[_MFORGIN._CELL4.index].type;

            if(_MFORGIN.makeEquip(equipInfo,mtype)){

                _GAMEDATAHELPER.reduceGoods(window.gameData.materialItem,_MFORGIN._CELL4.index,1);

                _MFORGIN.reduceM(makeconfig);

            }


        }else if(_MFORGIN._MAKETYPE==2){

            _MFORGIN.reduceM(makeconfig);

            _MFORGIN.makeItem(_MAKE[_MFORGIN._SOURCE].item);

        }

        _MFORGIN.refresh();
        _MG.refreshPage(_MG._CURRENTLABEL);
    },

    makeEquip:function(equipInfo,mtype){

        var mconfig = _M[mtype];

        if(mconfig.attr){

            for(var key in mconfig.attr){

                if(equipInfo.attr&&equipInfo.attr[key]){
                    _TIPS.showMessage('不可使用同类型的材料制作同一件装备！',0);
                    return false;
                }

            }

        }

        if(!equipInfo.maked){
            equipInfo.maked = 1;
        }else{
            equipInfo.maked++;
        }

        equipInfo.attr = _ROLEDATA.attrCombine(equipInfo.attr,_CU.cloneObj(mconfig.attr));

        _TIPS.showMessage('制作成功获得'+equipInfo.type+'*1',0);

        return true;
    },

    makeItem:function(type){

        _GAMEDATAHELPER.pushItem(_IOF.createObjByName(type,null));

        _TIPS.showMessage('制作成功获得'+type+'*1',0);

    },

    reduceM:function(makeconfig){

        for(var i=0;i<makeconfig.length;i++){
            var tempgoodsIndex = _GAMEDATAHELPER.getGoodsIndex(window.gameData.materialItem,makeconfig[i].type);
            var nmb = makeconfig[i].nmb;
            _GAMEDATAHELPER.reduceGoods(window.gameData.materialItem,tempgoodsIndex,nmb);
        }

    },

    clear:function(){
        var mainLabel = _MFORGIN._CURRENTLABEL.getChildByName('mainLabel');
        var messageNode = mainLabel.getChildByName('desc');
        var nameNode = mainLabel.getChildByName('name');
        var goodNode = mainLabel.getChildByName('arm').getChildByName('img');
        var goodsNode = mainLabel.getChildByName('goods');

        goodsNode.active = false;
        goodNode.getComponent(cc.Sprite).spriteFrame = null;
        nameNode.getComponent(cc.Label).string = "";
        messageNode.getComponent(cc.Label).string = "依次点击仓库里的格子放入制作书以及材料";
    },

    setBook:function(bookName){

        if(_MAKE[bookName]){
            _MFORGIN._SOURCE = bookName;
            var tempItem = _MAKE[bookName].item;
            if(_EQUIP[tempItem]){
                _MFORGIN._MAKETYPE=1;
            }else if(_FIGHTITEM[tempItem]){
                _MFORGIN._MAKETYPE=2;
            }
            _MFORGIN.refresh();
        }

    },

    canSet:function(type,goodsInfo){

        if(_MFORGIN._SOURCE==null){
            _TIPS.showMessage('请先选择制作书！',0);
            return false;
        }

        if(_MFORGIN._MAKETYPE!=1){
            _TIPS.showMessage('该制作书不需要此材料！',0);
            return false;
        }

        if(type==1&&goodsInfo.type!=_MAKE[_MFORGIN._SOURCE].item){
            _TIPS.showMessage('需要放入同名装备！',0);
            return false;
        }

        if(type==1&&goodsInfo.maked&&goodsInfo.maked>=_GAMEDATAHELPER.getMakeMax()){
            _TIPS.showMessage('当前制作次数:'+goodsInfo.maked+" 已超过制作上限次数:"+_GAMEDATAHELPER.getMakeMax(),0);
            return false;
        }

        if(type==2){

            //var makeconfig = _MAKE[_MFORGIN._SOURCE];
            var equipConfig = _EQUIP[_MAKE[_MFORGIN._SOURCE].item];
            var mconfig = _M[goodsInfo.type];

            if(equipConfig.pos!=mconfig.pos){
                _TIPS.showMessage('该材料不能制作这个部位的装备！',0);
                return false;
            }

        }

        return true;
    },

    setEquip:function(goodsInfo,goodsIndex){

        if(!_MFORGIN.canSet(1,goodsInfo)){
            return;
        }

        var mainLabel = _MFORGIN._CURRENTLABEL.getChildByName('mainLabel');
        var goodsNode = mainLabel.getChildByName('goods');
        var goodsArrNode = goodsNode.getChildren();
        _MFORGIN.refreshOneNode(goodsArrNode[2],_IOF.getGoodsImg(goodsInfo),goodsInfo.type+":1/1");
        _MFORGIN.setCell(window.gameData.equipItem,goodsIndex,3);
    },

    setM:function(goodsInfo,goodsIndex){

        if(!_MFORGIN.canSet(2,goodsInfo)){
            return;
        }

        var mainLabel = _MFORGIN._CURRENTLABEL.getChildByName('mainLabel');
        var goodsNode = mainLabel.getChildByName('goods');
        var goodsArrNode = goodsNode.getChildren();
        var goodsNmb = goodsInfo.nmb?goodsInfo.nmb:1;
        _MFORGIN.refreshOneNode(goodsArrNode[3],_IOF.getGoodsImg(goodsInfo),
                                goodsInfo.type+"("+_IOF.getGoodsAttrStr(goodsInfo)+")"+":"+goodsNmb+"/1");
        _MFORGIN.setCell(window.gameData.materialItem,goodsIndex,4);
    },

    refreshOneNode:function(node,img,des){
        var imgNode = node.getChildByName('arm').getChildByName('img');
        var imgAddNode = node.getChildByName('arm').getChildByName('img_add');
        var desNode = node.getChildByName('label');

        if(img!=null){
            imgNode.active = true;
            imgAddNode.active =false;
            imgNode.getComponent(cc.Sprite).spriteFrame = _GRM.getEquipFrame(img);
        }else{
            imgNode.active = false;
            imgAddNode.active =true;
        }
        desNode.getComponent(cc.Label).string = des;
    },

    refresh:function(){

        _MFORGIN.resetCell();

        var mainLabel = _MFORGIN._CURRENTLABEL.getChildByName('mainLabel');
        var messageNode = mainLabel.getChildByName('desc');
        var nameNode = mainLabel.getChildByName('name');
        var goodNode = mainLabel.getChildByName('arm').getChildByName('img');
        var goodsNode = mainLabel.getChildByName('goods');
        var goodsArrNode = goodsNode.getChildren();

        var makeconfig = _MAKE[_MFORGIN._SOURCE].arr;
        var targetItem = _MAKE[_MFORGIN._SOURCE].item;

        goodNode.getComponent(cc.Sprite).spriteFrame = _GRM.getEquipFrame(_IOF.getGoodsImg({type:targetItem}));
        nameNode.getComponent(cc.Label).string = targetItem;
        messageNode.getComponent(cc.Label).string = _MFORGIN._SOURCE;

        goodsNode.active = true;

        for(var i=0;i<4;i++){
            if(i>=makeconfig.length){
                goodsArrNode[i].active = false;
            }else{
                goodsArrNode[i].active = true;

                var tempgoodsIndex = _GAMEDATAHELPER.getGoodsIndex(window.gameData.materialItem,makeconfig[i].type);
                var tempgoodsNmb = 0;
                if(tempgoodsIndex!=-1){
                    if(window.gameData.materialItem[tempgoodsIndex].nmb){
                        tempgoodsNmb = window.gameData.materialItem[tempgoodsIndex].nmb;
                    }else{
                        tempgoodsNmb=1;
                    }
                }

                _MFORGIN.refreshOneNode(goodsArrNode[i],
                    _IOF.getGoodsImg({type:makeconfig[i].type}),
                    makeconfig[i].type+":"+tempgoodsNmb+"/"+makeconfig[i].nmb);
                
                _MFORGIN.setCell(window.gameData.materialItem,tempgoodsIndex,(i+1));
            }
        }

        if(_MFORGIN._MAKETYPE==1){
            goodsArrNode[2].active = true;
            goodsArrNode[3].active = true;
            _MFORGIN.refreshOneNode(goodsArrNode[2],null,"同名装备*1");
            _MFORGIN.refreshOneNode(goodsArrNode[3],null,"属性石头*1");
        }
    },


    setCell:function(arr,goodsIndex,cellIndex){

        _MFORGIN['_CELL'+cellIndex]={arr:arr,index:goodsIndex};

    },

    resetCell:function(){

        _MFORGIN._CELL1=null;
        _MFORGIN._CELL2=null;
        _MFORGIN._CELL3=null;
        _MFORGIN._CELL4=null;

    }

}